package com.coffee.websocket.command;

import com.coffee.core.CoffeeResponseEnum;
import com.coffee.core.ResponseCode;
import com.coffee.websocket.metadata.BitMessage;
import com.coffee.websocket.metadata.MochaBasicBinaryMessage;
import com.coffee.websocket.metadata.MochaBinaryProtocol;
import lombok.*;

import java.util.List;


/**
 * 前端传过来的 MochaMessage  转换成 MochaCommand 对象
 *
 * @author: songkui
 * @since: 2023/2/21 18:05
 */
@Setter
@Getter
public class MochaBinaryParam<T extends MochaBasicBinaryMessage> extends MochaBasicParam {
    private Long userId;

    private byte type;  //消息类型  1, chat  2, Binary
    private int cmd;
    private T request;

    private MochaBinaryProtocol protocol;


    public MochaBinaryParam(MochaBinaryProtocol protocol) {
        this.type = protocol.getType();
        this.cmd = protocol.getCmd();
        this.protocol = protocol;

    }


    public void sendMessage(ResponseCode code, MochaBasicBinaryMessage message) {
        protocol.setState(code.code());
        protocol.setData(message);
        sendMessage(protocol);
    }
    public void sendMessage(Integer code, MochaBasicBinaryMessage message) {
        protocol.setState(code);
        protocol.setData(message);
        sendMessage(protocol);
    }

    public void sendMessage(MochaBasicBinaryMessage message) {
        sendMessage(CoffeeResponseEnum.Success, message);
    }

    public void sendSuccess() {
        sendMessage(CoffeeResponseEnum.Success, BitMessage.build());
    }

    public void sendFailure(ResponseCode code) {
        sendMessage(code, BitMessage.build());
    }

    public void sendMessage(Long userId, MochaBasicBinaryMessage message) {
        protocol.setData(message);
        protocol.setState(CoffeeResponseEnum.Success.code());
        sendMessage(userId, protocol);
    }

    public void sendMessage(List<Long> idList, MochaBasicBinaryMessage message) {
        protocol.setData(message);
        protocol.setState(CoffeeResponseEnum.Success.code());
        sendMessage(idList, protocol);
    }

    public void sendMessageByAll(MochaBasicBinaryMessage message) {
        protocol.setData(message);
        protocol.setState(CoffeeResponseEnum.Success.code());
        sendMessageByAll(protocol);
    }

}
